package com.xiao.rabbitmq.utils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.text.SimpleDateFormat;


public class JsonTool {
    private static final ObjectMapper om = new ObjectMapper();

    static {
        om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }

    public static String getJsonStr(Object object) {
        try {
            if (object == null) return null;
            return om.writeValueAsString(object);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T getJsonObject(String json, Class<T> clazz) {
        try {
            if (json == null) return null;
            return om.readValue(json, clazz);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static <T> T getJsonObject(String json, TypeReference type) {
        try {
            if (json == null) return null;
            return om.readValue(json, type);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
